<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
  <style>
    .badge {
      float: right;
      margin-right: 5px;
    }

    .my-page {
      margin: 0 5px;
    }

    .all-page-content {
      margin-left: 5px;
    }
  </style>
</head>

<body style="padding: 15px;">

  <!-- 评论面板 -->
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">发表评论</h3>
    </div>
    <form class="panel-body cmt-form" autocomplete="off">
      <div>评论人：</div>
      <input type="text" class="form-control" name="username" autocomplete="off" id="sy-1" />
      <div>评论内容：</div>
      <textarea class="form-control" name="content" id="sy-2"></textarea>
      <button type="submit" class="btn btn-primary submit">发表评论</button>
    </form>
  </div>


  <!-- 评论列表 -->
  <ul class="list-group">
    <!-- <li class="list-group-item">
       <span>评论内容</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：xxx</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：xxx</span>
    </li> -->
  </ul>

  <!-- 分页器 -->
  <nav>
    <ul class="pagination">
      <li>
        <button class="last">
          <span>&laquo;</span>
        </button>
      </li>
      <li class="my-page">
        <span class="page-show"></span>
      </li>
      <li>
        <button class="next">
          <span>&raquo;</span>
        </button>
      </li>
      <li class="all-page-content">
        <span>共计:<span class="all-page"></span>页</span>
      </li>
    </ul>
  </nav>
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script>
    // 配置全局路径
    axios.defaults.baseURL = 'http://123.57.109.30:3006'
    // 获取所有评论并渲染
    function getBooks() {
      axios.get('/api/cmtlist', {
          params: {
            page: 1
          }
        })
        .then(({
          data: {
            data
          }
        }) => {
          console.log(data)
          const str = data.map(({
            content,
            id,
            time,
            username
          }) => `
    <li class="list-group-item">
       <span>${content}</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;" data-index='${id}'>删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：${time}</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：${username}</span>
    </li>
    `).join('')
          document.querySelector('.list-group').innerHTML = str
          console.log(str);
        })
    }
    getBooks()
    // 新增评论功能
    const f2 = document.querySelector('.panel-body')
    f2.addEventListener('submit', function () {
      const fd = new FormData(f2)
      axios.post('/api/addcmt', fd)
        .then(res => {
          // console.log(res)
          getBooks()
        })


    })
  </script>
</body>

</html>